#include<string>
#include<iostream>
struct free_throws
{
    std::string name;
    int made;
    int attempts;
    float percent;
};

void display(const free_throws & ft);
void set_pc(free_throws & ft);
const free_throws & accumulate(free_throws & target, const free_throws & source);

int main_strc_ref(int argc, char const *argv[])
{
    free_throws one = {"Ifelsa Branch", 13, 14};
    free_throws two = {"Andor Knott", 10, 16};
    free_throws three = {"Minnie Max", 7, 9};
    free_throws four = {"Whily Looper", 5, 9};
    free_throws five = {"Long Long", 6, 14};
    free_throws team = {"Throwgoods", 0, 0};

    set_pc(one);
    display(one);
    accumulate(team,one);
    display(team);
    
    display(accumulate(team,two));
    // 编译错误，因为accumulate返回的是const类型的
    // accumulate(accumulate(team, three),four);
    display(team);

    free_throws dup;
    dup = accumulate(team, five);
    std::cout << "Display team:\n";
    display(team);
    std::cout << "Display dup after assignment:\n";
    display(dup);

    set_pc(four);
    // 编译错误，因为accumulate返回的是const类型的
    // accumulate(dup,five) = four;
    std::cout << "Displaying dup after ill-assignment:\n";
    display(dup);
    return 0;
}

void display(const free_throws & ft) {
    using std::cout;
    using std::endl;
    cout << "Name:" << ft.name << "\n"
         << "Made:" << ft.made << "\t"
         << "Attempts:" << ft.attempts << "\t"
         << "Percent:" << ft.percent << endl;

}

void set_pc(free_throws & ft) {
    if (ft.attempts != 0)
    {
        ft.percent = 100 * (float(ft.made) / float(ft.attempts));
    }else{
        ft.percent = 0;
    }
}

const free_throws & accumulate(free_throws & target, const free_throws & source) {
    target.made += source.made;
    target.attempts += source.attempts;
    set_pc(target);
    return target;
}


